import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    //展示页面路由
    {
      path: '/',
      name: 'index',
      component:  r => require.ensure([], () => r(require('@/views/front/Index')), 'Index'),
      redirect:'/home',
      children:[
        {
          path: 'home',
          name: 'home',
          component:  r => require.ensure([], () => r(require('@/views/front/Home')), 'Home'),
          meta: {
            title: "主页",    
          }},
            {
              path:'technology',
              name:'technology',
              component:  r => require.ensure([], () => r(require('@/views/front/Technology')), 'Technology'),
              meta: {
                title: "技术杂谈",    
              }
            },
            {
              path:'life',
              name:'life',
              component:  r => require.ensure([], () => r(require('@/views/front/Life')), 'Life'),
              meta: {
                title: "生活随笔",    
              }
            },
            // {
            //   path:'msgboard',
            //   name:'msgboard',
            //   component:  r => require.ensure([], () => r(require('@/views/front/MsgBoard')), 'MsgBoard'),
            //   meta: {
            //     title: "留言板",    
            //   }
            // },
            {
              path:'about',
              name:'about',
              component:  r => require.ensure([], () => r(require('@/views/front/About')), 'About'),
              meta: {
                title: "关于",    
              }
            },
            {
              path:'article/:id',
              name:'article',
              component:  r => require.ensure([], () => r(require('@/views/front/Article')), 'Article'),
              meta: {
                title: "文章展示",    
              }
            },
            {
              path:'search',
              component:  r => require.ensure([], () => r(require('@/views/front/ArticleBy')), 'ArticleBy'),
              meta: {
                title: "标签下的文章",    
              }
            },
      ]
    },
    //管理页面路由
    {
      path: '/admin',
      name: 'manage',
      component: () => import( '../views/behind/Home.vue'),
      meta:{requiresAuth: true},
      children:[
        {
          path: '/create/article',
          component: () => import( '../views/behind/ArticleWrite.vue'),
          meta:{requiresAuth: true},
        },
        {
          path: '/create/saying',
          component: () => import( '../views/behind/SayingWrite.vue'),
          meta:{requiresAuth: true},
        },
        {
          path: '/manage/article',
          component: () => import( '../views/behind/ArticleManage.vue'),
          meta:{requiresAuth: true},
        },
        {
          path: '/manage/saying',
          component: () => import( '../views/behind/SayingManage.vue'),
          meta:{requiresAuth: true},
        },
        {
          path: '/manage/user',
          component: () => import( '../views/behind/UserManage.vue'),
          meta:{requiresAuth: true},
        },
        {
          path: '/manage/link',
          component: () => import( '../views/behind/FriendlyLink.vue'),
          meta:{requiresAuth: true},
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import( '../views/behind/Login.vue')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import( '../views/behind/Register.vue')
    },
    {
      path: '*',
      name: '404',
      component: () => import( '../views/behind/404.vue')
    }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    let token = localStorage.getItem('Authorization');
    if (!token) {
      next({
        path: '/login',
      })
    } else {
      next()
    }
  } else {
    next() // 确保一定要调用 next()
  }
});

export default router
